# coding=utf8
from werobot.replies import ArticlesReply, Article
from werobot import WeRoBot

MAIN_IP = 'www.tengweidianzi.com'
APP_ID = 'wxc9433e4ffbb31474'
APP_SECRET = 'd7de31e3244c8fd6fa1df5f59e62d20a'
#MAIN_URL = "http://115.159.26.93/robotLock/"
MAIN_URL = "http://" + MAIN_IP + "/robotLock/"
REDIRECT_URI = 'http://' + MAIN_IP + '/robotLock/wechat_auth'
AUTH_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APP_ID + '&redirect_uri=' + REDIRECT_URI + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'

robot = WeRoBot(token='robotLock',APP_ID=APP_ID, APP_SECRET=APP_SECRET)
client = robot.client

def show_all_service(message):
    reply = ArticlesReply(message=message)
    article = Article(
        title=u"智码开门",
        description=u"智码开门是智能动态密码开锁",
        img="http://chenguanfuqq.oschina.io/tuquan/img_2017_6/xiamu.jpg",
        url="http://" + MAIN_IP + ":8001"
    )
    reply.add_article(article)
    article = Article(
        title=u"远程开锁",
        description=u"远程开锁可免去携带钥匙，方便接待宾客",
        img="http://chenguanfuqq.oschina.io/tuquan/img_2017_6/xiamu.jpg",
        url="http://" + MAIN_IP + ":8001"
    )
    reply.add_article(article)
    article = Article(
        title=u"微信网页授权调试",
        description=u"微信网页授权调试",
        img="http://chenguanfuqq.oschina.io/tuquan/img_2017_6/xiamu.jpg",
        url=AUTH_URL
    )
    reply.add_article(article)
    article = Article(
        title=u"投票界面",
        description=u"微信投票界面调试",
        img="http://chenguanfuqq.oschina.io/tuquan/img_2017_6/xiamu.jpg",
        url="http://" + MAIN_IP + ":8002/base.html"
    )
    reply.add_article(article)
    return reply

def show_message_info(message):
    print "message_id:" + str(message.message_id)
    print "target:" + str(message.target)
    print "source:" + str(message.source)
    print "time:" + str(message.time)

client.create_menu({
    "button":[
    {
        "name":u"公司",
        "sub_button":[
            {
                "type":"view",
                "name":u"公司简介",
                "url": MAIN_URL + "company_info"
            },
            {
                "type":"view",
                "name":u"经典案例",
                "url": MAIN_URL + "simple_case"
            }
        ]
    },
    {
        "name":u"产品",
        "sub_button":[
            {
                "type":"view",
                "name":u"新品上市",
                "url": MAIN_URL + "new_product"
            },
            {
                "type":"view",
                "name":u"淘宝新品",
                "url": MAIN_URL + "taobao_new_prouct"
            }
        ]
    },
    {
        "name":u"服务",
        "sub_button":[
            {
                "type":"view",
                "name":u"APP下载",
                "url": MAIN_URL + "app_download"
            },
            {
                "type":"view",
                "name":u"联系我们",
                "url": MAIN_URL + "contact_us"
            },
            {
                "type":"click",
                "name":u"服务支持",
                "key": "service_support"
                #"url": MAIN_URL + "service_support"
            }
        ]
    }
]})

@robot.subscribe
def subscribe(message):
    return '感谢关注腾威电子科技!'

@robot.key_click("company_info")
def company_info(message):
    return u'公司简介'

@robot.key_click("simple_case")
def simple_case(message):
    return u'经典案例'

@robot.key_click("new_product")
def new_product(message):
    return u'新品上市'

@robot.key_click("taobao_new_prouct")
def taobao_new_prouct(message):
    return u'淘宝新品'

#@robot.key_click("app_download")
#def app_download(message):
#    return u'APP下载'

@robot.key_click("contact_us")
def contact_us(message):
    return u'联系我们'

@robot.key_click("service_support")
def service_support(message):
    return show_all_service(message)
    #return u'服务支持'

#@robot.handler
#def hello(message):
#    return 'Hello world'

@robot.text
def echo(message):
    #client.send_text_message(message.source, "echo")
    #show_message_info(message)
    if message.content == u'服务':
        return show_all_service(message)
    return message.content

@robot.image
def img(message):
    #show_message_info(message)
    return message.img

